﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DevExpress.Web.ASPxGridView;
using System.Drawing;

public partial class GuideInfo : System.Web.UI.Page
{
    private DataTable _dtGuideArrangeInfo;

    public DateTime _StartDate { get { return DateTime.Parse(Session["startdate"].ToString()); } set { Session["startdate"] = value; } }
    public DateTime _EndDate { get { return DateTime.Parse(Session["enddate"].ToString()); } set { Session["enddate"] = value; } }
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            tbSelectedDate.Date = DateTime.Now;

            DateTime currentSelectDate = DateTime.Parse(tbSelectedDate.Date.ToString());//new DateTime(2012, 2, 20);
            _StartDate = new DateTime(currentSelectDate.Year, currentSelectDate.Month, 1);
            _EndDate = new DateTime(currentSelectDate.Year, currentSelectDate.Month, DateTime.DaysInMonth(currentSelectDate.Year, currentSelectDate.Month));

            //初始化添加语言项
            foreach (DataRow dr in BaseInfoCtrl.GetCiceroniLanguageData().Rows)
                tbLanguageEdit.Items.Add(dr[0].ToString());
            LoadGuideArrangeLayout();
        }
    }

    private void LoadGuideArrangeLayout()
    {
        //guideStatusGridView.DataSource=db 
        //根据导游语言筛选导游，并生成网格
        string language = tbLanguageEdit.Text;

        DataTable tmpDt = BaseInfoCtrl.GetCiceroniNameData(language);
        guideStatusGridView.Columns.Clear();

        for (int i = 0; i < (_EndDate - _StartDate).Days + 1; i++)
        {
            tmpDt.Columns.Add(_StartDate.AddDays(i).ToString("yyyy-MM-dd"));//.Caption = _StartDate.AddDays(i).ToString("MM<br>dd");//_StartDate.AddDays(i).Day.ToString();
        }

        foreach (DataColumn dataColumn in tmpDt.Columns)
        {
            GridViewDataTextColumn column = new GridViewDataTextColumn();
            column.FieldName = dataColumn.ColumnName;
            // set additional column properties
            switch (dataColumn.ColumnName)
            {
                case "CiceroniID":
                case "CiceroniName":
                    //column.DataItemTemplate = new ItemTemplate(dataColumn.ColumnName);
                    break;
                default:
                    column.DataItemTemplate = new ItemTemplate(dataColumn.ColumnName);
                    string[] captions = dataColumn.ColumnName.Split(new char[] { '-' });
                    column.Caption = string.Format("{0}<br>{1}", captions[1], captions[2]);
                    break;
            }
            guideStatusGridView.Columns.Add(column);
        }
        
        guideStatusGridView.Columns["CiceroniID"].Visible = false;              //导游CiceroniID列不显示
        guideStatusGridView.Columns["CiceroniName"].Caption = "姓　名";
        //guideStatusGridView.Columns["CiceroniName"].FixedStyle = GridViewColumnFixedStyle.Left;
        
        //根据月份获取已经安排的导游信息
        _dtGuideArrangeInfo = GuideArrangeCtrl.GetGuideArrangeData(_StartDate, _EndDate);//_CurrentSelectDate);
        if (_dtGuideArrangeInfo == null)
        {
            //刷新网格
            guideStatusGridView.DataSource = tmpDt;
            guideStatusGridView.DataBind();
            return;
        }
        foreach (DataRow dr in _dtGuideArrangeInfo.Rows) //循环导游安排信息
        {
            DataRow[] tmpDr = tmpDt.Select(string.Format("CiceroniID={0}", dr["CiceroniID"].ToString()));//将已安排导游ID做为过滤条件，找到将要赋值的行
            if (tmpDr != null && tmpDr.Length > 0)
            {
                DateTime start = DateTime.Parse(dr["StartDate"].ToString());
                DateTime end = DateTime.Parse(dr["EndDate"].ToString());

                int days = (end - start).Days + 1;//int.Parse(end.ToString("dd")) - int.Parse(start.ToString("dd"))+1;
                for (int i = 0; i < days; i++)//(end - start).Days + 1; i++)
                {
                    //察看事件类型，是事假则赋值0
                    switch (dr["HasEvent"].ToString())
                    {
                        case "1"://事假
                            if (i == 0)//是否为起始位置
                                tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "#0";
                            else
                                tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "0";
                            break;
                        case "5"://紧急事件
                            if (i == 0)
                                tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "#5";
                            else
                                tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "5";
                            break;
                        default:
                            string cellValue = tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")].ToString();//察看界面是否已经有赋值，存在多次上团的可能性
                            if (cellValue == "")//表明还没有标记，此次为导游一天内第1次上团
                            {
                                if (i == 0)
                                    tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "#1";
                                else
                                    tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "1";
                                continue;
                            }
                            if (cellValue == "1")//表明还没有标记，此次为导游一天内重复第2次上团
                            {
                                if (i == 0)
                                    tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "#2";
                                else
                                    tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "2";
                                continue;
                            }
                            if (cellValue == "2")//表明还没有标记，此次为导游一天内重复第3次上团
                            {
                                if (i == 0)
                                    tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "#3";
                                else
                                    tmpDr[0][start.AddDays(i).ToString("yyyy-MM-dd")] = "3";
                                continue;
                            }
                            break;
                    }
                   
                }
            }
        }
        guideStatusGridView.DataSource = tmpDt;
        guideStatusGridView.DataBind();

    }
    protected void ASPxGridView1_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
    {
        if (e.CellValue==null || e.DataColumn.Index == 0 || e.CellValue == DBNull.Value) return;

        switch (e.CellValue.ToString())
        {
            case "#1":
            case "1"://团安排导游
                e.Cell.BackColor = Color.Green;
                break;
            case "#0":
            case "0"://导游事假
                e.Cell.BackColor = Color.Yellow;
                break;
            case "#2":
            case "2"://导游一天内重复2次上团
                e.Cell.BackColor = Color.Blue;
                break;
            case "#3":
            case "3"://导游一天内重复3次上团
                e.Cell.BackColor = Color.Brown;
                break;
            case "#5":
            case "5":
                e.Cell.BackColor = Color.Red;
                break;
            default:
                break;
        }
    }

    //protected void btnBack_Click(object sender, EventArgs e)
    //{
    //    _StartDate = _StartDate.AddDays(-1);
    //    _EndDate = _EndDate.AddDays(-1);
    //    LoadGuideArrangeLayout();
       
    //}
    //protected void btnForward_Click(object sender, EventArgs e)
    //{
    //    _StartDate = _StartDate.AddDays(1);
    //    _EndDate = _EndDate.AddDays(1);
    //    LoadGuideArrangeLayout();
    //}

    //protected void tbSelectedDate_DateChanged(object sender, EventArgs e)
    //{
    //    DateTime currentSelectDate = DateTime.Parse(tbSelectedDate.Date.ToString());//new DateTime(2012, 2, 20);
    //    _StartDate = new DateTime(currentSelectDate.Year, currentSelectDate.Month, 1);
    //    _EndDate = new DateTime(currentSelectDate.Year, currentSelectDate.Month, DateTime.DaysInMonth(currentSelectDate.Year, currentSelectDate.Month));
    //    LoadGuideArrangeLayout();
    //}
    //protected void tbLanguageEdit_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    LoadGuideArrangeLayout();
    //}

    protected void cbServerPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        switch (e.Parameter)
        {
            case "Date"://日期选择激发了
                DateTime currentSelectDate = DateTime.Parse(tbSelectedDate.Date.ToString());//new DateTime(2012, 2, 20);
                _StartDate = new DateTime(currentSelectDate.Year, currentSelectDate.Month, 1);
                _EndDate = new DateTime(currentSelectDate.Year, currentSelectDate.Month, DateTime.DaysInMonth(currentSelectDate.Year, currentSelectDate.Month));
                break;
            case "Language"://语言选择激发
                break;
            case "Forward"://向前按钮激发
                _StartDate = _StartDate.AddDays(1);
                _EndDate = _EndDate.AddDays(1);
                break;
            case "Back"://向后按钮激发
                _StartDate = _StartDate.AddDays(-1);
                _EndDate = _EndDate.AddDays(-1);
                break;
            default:
                break;
        }
        LoadGuideArrangeLayout();
    }

    protected void guideStatusGridView_PageIndexChanged(object sender, EventArgs e)
    {
        LoadGuideArrangeLayout();
    }
}
